package table_host;

import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;

import dialog.CustomDialog;
import entity.ContactEntity;

import tangyaxin.www.saoerweima.ActivityText;
import tangyaxin.www.saoerweima.ActivityUrl;
import tangyaxin.www.saoerweima.BaseAcitivity;
import tangyaxin.www.saoerweima.EncodeContactActivity;
import tangyaxin.www.saoerweima.QRMakeActivity;
import tangyaxin.www.saoerweima.R;


/**
 * Created by Administrator on 2016/6/7.
 */
public class MakeActivity extends BaseAcitivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frament_make);
        findview();
        initview();

    }

    private Dialog makeContactDialog;
    private View makeContactBtn,makeUrlBtn,makeTextBtn;
    @Override
    protected void findview() {
        makeContactBtn=findViewById(R.id.make_contact);
        makeUrlBtn=findViewById(R.id.make_url);
        makeTextBtn=findViewById(R.id.make_text);
    }

    @Override
    protected void initview() {
        makeContactBtn.setOnClickListener(onClickListener);
        makeUrlBtn.setOnClickListener(onClickListener);
        makeTextBtn.setOnClickListener(onClickListener);
        initdialog();
    }

    private void initdialog() {
        makeContactDialog=new CustomDialog(this,R.layout.make_dialog);
        makeContactDialog.findViewById(R.id.choose_cantact_btn).setOnClickListener(onClickListener);
        makeContactDialog.findViewById(R.id.new_cantact_btn).setOnClickListener(onClickListener);
        makeContactDialog.findViewById(R.id.close_btn).setOnClickListener(onClickListener);

    }
    private View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.make_contact:
                    makeContactDialog.show();
                    break;
                case R.id.make_url:
                    startActivity(new Intent(MakeActivity.this, ActivityUrl.class));
                    break;
                case R.id.make_text:
                    startActivity(new Intent(MakeActivity.this, ActivityText.class));
                    break;
                case R.id.choose_cantact_btn:
                    intentContacyActivity();
                    break;
                case R.id.new_cantact_btn:

                    break;
                case R.id.close_btn:
                    makeContactDialog.dismiss();
                    break;
            }
        }
        private void intentContacyActivity() {
            Intent intent=new Intent();
            intent.setAction(Intent.ACTION_PICK);
            intent.setData(ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(intent,100);
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode!=100||requestCode!=RESULT_OK)return;
        Uri uri=data.getData();
        ContactEntity contactEntity=new ContactEntity();
        Cursor cursor=getContentResolver().query(uri,null,null,null,null);
        if (cursor==null)return;
        String contactId=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        cursor.close();
        contactEntity.setName(name);
        Cursor phoneCur=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID+" = " +contactId,null,null);
        while (phoneCur!=null && phoneCur.moveToNext()){
            String phone=phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            contactEntity.getPhoneNums().add(phone);
        }
        phoneCur.close();
        Cursor emailCur=getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID+" = "+contactId,null,null);
        while (emailCur!=null && emailCur.moveToNext()){
            String email=emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            contactEntity.getEmails().add(email);
        }
        emailCur.close();
        String whereStr=ContactsContract.Data.CONTACT_ID+" = "+contactId+"AND"+ContactsContract.Data.MIMETYPE
                +"="+ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE;
        Cursor addressCur=getContentResolver().query(ContactsContract.Data.CONTENT_URI,null,whereStr,null,null);
        if (addressCur!=null&&addressCur.moveToNext()){
            contactEntity.setAddress(addressCur.getString(addressCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET)));
        }
        addressCur.close();
        Intent intent=new Intent(MakeActivity.this, EncodeContactActivity.class);
        intent.putExtra(EncodeContactActivity.KEY_CONTACT_ENTITY,contactEntity);
        startActivity(intent);
    }
}
